package com.appspot.moviesessions;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MovieListPresenter {

	private MovieListView view;
	private LoaderFactory loaderFactory;
	private IntentFactory intentFactory;
	private int movieListPosition;

	public MovieListPresenter(MovieListView view, LoaderFactory loaderFactory,
			IntentFactory intentFactory) {
		this.view = view;
		this.loaderFactory = loaderFactory;
		this.intentFactory = intentFactory;

	}

	public void onCreate() {
		// TODO Auto-generated method stub
	}

	public void onStart() {
		setupMovieList();
		registerItemClickListener();
	}

	private void registerItemClickListener() {
		view.getMovieList().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View theView,
					int position, long id) {
				view.startActivity(intentFactory.launchMovieInfoIntent(
						view.getContext(), id));

			}
		});
	}

	private void setupMovieList() {
		Loader movieListLoader = loaderFactory.movieListLoader();
		view.getSupportLoaderManager().initLoader(0x01, null, movieListLoader);
		view.getMovieList().setAdapter(movieListLoader);
	}

	public void onPause() {
		movieListPosition = view.getMovieList().getFirstVisiblePosition();

	}

	public void onResume() {
		view.getMovieList().setSelection(movieListPosition);
	}

	public void onDestroy() {
		// TODO Auto-generated method stub

	}

}
